package com.timber.panels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;

import pv.jfcx.JPVCurrency;
import pv.jfcx.JPVDate;
import pv.jfcx.JPVDatePlus;

import com.borland.jbcl.layout.VerticalFlowLayout;

import com.timber.frame.ImsFrame;
import com.timber.tableModels.InventoryTableModel;
import com.timber.util.Global;
import com.timber.util.ImageMgr;
import com.timber.util.ImsFrameComponent;
import com.timber.util.InventoryMgr;
import com.timber.util.SLTablePrinter;
import com.timber.util.SLTableView;
import com.timber.util.SearchableComboBox;
import com.timber.util.TimberNameMgr;

public class InvPanel extends ImsFrameComponent {

	
	  protected InventoryTableModel	lnDetailModel = new InventoryTableModel();
	 
	  
	  protected BorderLayout		borderLayout1 = new BorderLayout();
	  protected JPanel			topPanel = new JPanel();
	  protected GridBagLayout		gridBagLayout1 = new GridBagLayout();
	  protected JLabel			classLabel = new JLabel();
	  protected JPanel			centerPanel = new JPanel();
	  protected Border			border1;
	  protected TitledBorder		titledBorder1;
	  protected GridBagLayout		gridBagLayout2 = new GridBagLayout();
	  protected JLabel			timberNameLabel = new JLabel();
	  protected SearchableComboBox		nameCB;
	  protected SearchableComboBox		classCB;
	  protected JLabel			recDateFromLabel = new JLabel();
	  protected JPVDatePlus			recDateFromDP = new JPVDatePlus();
	  protected JLabel			recDateToLabel = new JLabel();
	  protected JPVDatePlus			recDateToDP = new JPVDatePlus();
	  protected JPanel			btnPanel = new JPanel();
	  protected JPanel			btnHolder = new JPanel();
	  protected VerticalFlowLayout		verticalFlowLayout1 = new VerticalFlowLayout();
	  protected FlowLayout			flowLayout1 = new FlowLayout();
	  protected JButton			clearBtn = new JButton();
	  protected JButton			goBtn = new JButton();
	  protected ImageIcon			goImage;
	  protected ImageIcon			clearAllImage;

	  protected SLTableView			inDetailTV = new SLTableView( lnDetailModel );
	  protected SLTablePrinter		aPrinter;
	  protected Color			focusCellColor = new Color( 204, 204, 255 );
	  private   JMenuItem                   addCorpAction  = new JMenuItem("Add Cash Dividend");
	  private   JMenuItem                   updCorpAction  = new JMenuItem("Update Cash Dividend");
	  
	  protected JPVCurrency LengthTF = new JPVCurrency();
	  protected JLabel LengthLabel; 
	  protected JPVCurrency widthTF = new JPVCurrency();
	  protected JLabel 		widthLabel;
	  

	  public InvPanel( ImsFrame parent )
	  {
	    super( parent );
	    try
	    {
	      jbInit();
	    }
	    catch(Exception ex)
	    {
	      ex.printStackTrace();
	    }
	  }
	  
	  void jbInit() throws Exception
	  {

		 

		    goImage       = ImageMgr.getIcon( ImageMgr.GO32_ICON );
		    clearAllImage = ImageMgr.getIcon( ImageMgr.CLEARALL32_ICON );
		    this.setLayout( borderLayout1 );
		    border1 = BorderFactory.createEtchedBorder( Color.white, new Color( 142, 142, 142 ) );
		    titledBorder1 = new TitledBorder( border1, " Inventory Details" );
		    topPanel.setLayout( gridBagLayout1 );
		    classLabel.setText( "T Class" );
		    centerPanel.setBorder( titledBorder1 );
		    centerPanel.setLayout( gridBagLayout2 );
		    timberNameLabel.setText( "T name" );
		    recDateFromLabel.setText( "From Rec Date" );
		    recDateToLabel.setText( "To Rec Date" );

		    btnHolder.setLayout( verticalFlowLayout1 );

		    flowLayout1.setAlignment( FlowLayout.RIGHT );
		    btnPanel.setLayout( flowLayout1 );

		    goBtn.setIcon( goImage );
		    goBtn.setMargin( new Insets( 0, 0, 0, 0 ) );
		    goBtn.addActionListener( new ActionListener()
		      {
			public void actionPerformed( ActionEvent e )
			{
			  lookupCorpActions( );
			}

			
		      } );

		    clearBtn.setIcon( clearAllImage );
		    clearBtn.setMargin( new Insets( 0, 0, 0, 0 ) );
		    clearBtn.setToolTipText( "Clear All" );
		    clearBtn.addActionListener( new ActionListener()
		      {
			public void actionPerformed( ActionEvent e )
			{
			  clear();
			}

			private void clear() {
				System.out.println("Called clear option");
				
			}
		      } );
		    
		 
		    nameCB = new SearchableComboBox( Global.getMainFrame(),
		    		TimberNameMgr.mgr.getDataVector(),
							SearchableComboBox.INCLUDE_ALL,
							true );
		    nameCB.setEditable( true );
		    nameCB.setPreferredSize( new Dimension( 130, 21 ) );

		    classCB = new SearchableComboBox( Global.getMainFrame(),
		    		TimberNameMgr.mgr.getDataVector(),
						     SearchableComboBox.INCLUDE_ALL,
						     true );
		    classCB.setEditable( true );
		    classCB.setPreferredSize( new Dimension( 130, 21 ) );

		    recDateFromDP.setDate( null );
		    recDateFromDP.setFormat( JPVDate.DMY );
		    recDateFromDP.setMonthFormat( JPVDate.MMM );
		    recDateFromDP.setSeparator( "-" );
		    recDateFromDP.setCloseOnDoubleClick( false );
		    recDateFromDP.addFocusListener( new FocusAdapter()
		      {
			public void focusGained( FocusEvent fe )
			{
			  recDateFromDP.setCaretPosition( 0 );
			}
		      } );

		    recDateToDP.setDate( null );
		    recDateToDP.setFormat( JPVDate.DMY );
		    recDateToDP.setMonthFormat( JPVDate.MMM );
		    recDateToDP.setSeparator( "-" );
		    recDateToDP.setCloseOnDoubleClick( false );
		    recDateToDP.addFocusListener( new FocusAdapter()
		      {
			public void focusGained( FocusEvent fe )
			{
			  recDateToDP.setCaretPosition( 0 );
			}
		      } );

		    LengthLabel = new JLabel("Length");
		    widthLabel = new JLabel("Width"); 
			Dimension d = new Dimension(80,20);
		    LengthTF.setDecimals(4);
		    LengthTF.setSymbol("");
		    LengthTF.setText("");        
		    LengthTF.setPreferredSize(d);
		    LengthTF.setMinimumSize(d);
		    LengthTF.setAlignment(2);
		    
		   widthTF.setDecimals(4);
		   widthTF.setSymbol("");
		   widthTF.setText("");        
		   widthTF.setPreferredSize(d);
		   widthTF.setMinimumSize(d);
		   widthTF.setAlignment(2);
		   
		    this.add( topPanel, BorderLayout.NORTH );
//		    topPanel.add( securityChooser,
//				  new GridBagConstraints( 0,
//							  0,
//							  2,
//							  2,
//							  0.0,
//							  0.0,
//							  GridBagConstraints.NORTHWEST,
//							  GridBagConstraints.NONE,
//							  new Insets( 5, 10, 0, 0 ), 0, 0 ) );
		    topPanel.add( timberNameLabel,
				  new GridBagConstraints( 2,
							  0,
							  1,
							  1,
							  0.0,
							  0.0,
							  GridBagConstraints.SOUTHWEST,
							  GridBagConstraints.NONE,
							  new Insets( 10, 5, 0, 0 ), 0, 0 ) );
		    topPanel.add( nameCB,
				  new GridBagConstraints( 2,
							  1,
							  1,
							  1,
							  0.0,
							  0.0,
							  GridBagConstraints.NORTHWEST,
							  GridBagConstraints.NONE,
							  new Insets( 0, 5, 0, 0 ), 0, 0 ) );
		    topPanel.add( classLabel,
				  new GridBagConstraints( 3,
							  0,
							  1,
							  1,
							  0.0,
							  0.0,
							  GridBagConstraints.SOUTHWEST,
							  GridBagConstraints.NONE,
							  new Insets( 10, 15, 0, 0 ), 0, 0 ) );
		    topPanel.add( classCB,
				  new GridBagConstraints( 3,
							  1,
							  1,
							  1,
							  0.0,
							  0.0,
							  GridBagConstraints.NORTHWEST,
							  GridBagConstraints.NONE,
							  new Insets( 0, 15, 0, 0 ), 0, 0 ) );
		    topPanel.add( recDateFromLabel,
				  new GridBagConstraints( 4,
							  0,
							  1,
							  1,
							  0.0,
							  0.0,
							  GridBagConstraints.SOUTHWEST,
							  GridBagConstraints.NONE,
							  new Insets( 12, 40, 0, 0 ), 0, 0 ) );
		    topPanel.add( recDateFromDP,
				  new GridBagConstraints( 4,
							  1,
							  1,
							  1,
							  0.0,
							  0.0,
							  GridBagConstraints.NORTHWEST,
							  GridBagConstraints.NONE,
							  new Insets( 0, 40, 0, 0 ), 0, 0 ) );
		    topPanel.add( recDateToLabel,
				  new GridBagConstraints( 5,
							  0,
							  1,
							  1,
							  0.0,
							  0.0,
							  GridBagConstraints.SOUTHWEST,
							  GridBagConstraints.NONE,
							  new Insets( 12, 10, 0, 0 ), 0, 0 ) );
		    topPanel.add( recDateToDP,
				  new GridBagConstraints( 5,
							  1,
							  1,
							  1,
							  0.0,
							  0.0,
							  GridBagConstraints.NORTHWEST,
							  GridBagConstraints.NONE,
							  new Insets( 0, 10, 0, 0 ), 0, 0 ) );
		    
	//TODO
		    
		    
		    topPanel.add( LengthLabel,
					  new GridBagConstraints( 6,
								  0,
								  1,
								  1,
								  0.0,
								  0.0,
								  GridBagConstraints.SOUTHWEST,
								  GridBagConstraints.NONE,
								  new Insets( 12, 10, 0, 0 ), 0, 0 ) );
			    topPanel.add( LengthTF,
					  new GridBagConstraints( 6,
								  1,
								  1,
								  1,
								  0.0,
								  0.0,
								  GridBagConstraints.NORTHWEST,
								  GridBagConstraints.NONE,
								  new Insets( 0, 10, 0, 0 ), 0, 0 ) );
			    
			    
			    topPanel.add( widthLabel,
						  new GridBagConstraints( 7,
									  0,
									  1,
									  1,
									  0.0,
									  0.0,
									  GridBagConstraints.SOUTHWEST,
									  GridBagConstraints.NONE,
									  new Insets( 12, 10, 0, 0 ), 0, 0 ) );
				    topPanel.add( widthTF,
						  new GridBagConstraints( 7,
									  1,
									  1,
									  1,
									  0.0,
									  0.0,
									  GridBagConstraints.NORTHWEST,
									  GridBagConstraints.NONE,
									  new Insets( 0, 10, 0, 0 ), 0, 0 ) );
				    
			    
		    
		    
		    
		    topPanel.add( btnPanel,
				  new GridBagConstraints( 8,
							  0,
							  GridBagConstraints.REMAINDER,
							  GridBagConstraints.REMAINDER,
							  100.0,
							  100.0,
							  GridBagConstraints.CENTER,
							  GridBagConstraints.BOTH,
							  new Insets( 5, 10, 0, 0 ), 0, 0 ) );
		    
		    
		    
		    
		    
		    
		    btnPanel.add(btnHolder, null);
		    btnHolder.add(goBtn, null);
		    btnHolder.add(clearBtn, null);

		    
//		    topPanel.add( getSecClass(),
//		              new GridBagConstraints( 0,
//		                          1,
//		                          1,
//		                          1,
//		                          0.0,
//		                          0.0,
//		                          GridBagConstraints.CENTER,
//		                          GridBagConstraints.NONE,
//		                          new Insets( 5, 5, 0, 0 ), 0, 0 ) );

		    this.add( centerPanel, BorderLayout.CENTER );

		    addCorpAction.addActionListener(new java.awt.event.ActionListener(){
		          public void actionPerformed(ActionEvent e) {
		            addCorpAction_actionPerformed(e);
		          }

				private void addCorpAction_actionPerformed(ActionEvent e) {
					System.out.println("addCorpAction_actionPerformed event is called ");
					
				}
		    });
		    updCorpAction.addActionListener(new java.awt.event.ActionListener(){
		      public void actionPerformed(ActionEvent e) {
		        updCorpAction_actionPerformed(e);
		      }

			private void updCorpAction_actionPerformed(ActionEvent e) {
				System.out.println("updCorpAction_actionPerformed event is called ");
				
			}
		    });

		    setUpInDetailTV();
		    addInDetailTV();
		  
		  
	  }
	  
	  
	  protected void setUpInDetailTV()
	  {
	    inDetailTV.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS );
	    inDetailTV.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
	    inDetailTV.setEditable( false );
	    inDetailTV.setEnableSortData( true );
	    inDetailTV.setSortingNulls( true );
	    inDetailTV.setShowSortIcons( true );
	    inDetailTV.setScrollAfterSort( false );
	    inDetailTV.getTable().setRowSelectionAllowed( true );
	    inDetailTV.getTable().setSelectionMode( ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );
	    inDetailTV.setFocusCellBackground( focusCellColor );

	    inDetailTV.getPopupMenu().add(addCorpAction);
	    inDetailTV.getPopupMenu().add(updCorpAction);
	    inDetailTV.addRowSelectionListener(new javax.swing.event.ListSelectionListener() {
	        public void valueChanged(ListSelectionEvent e) {
	            // Update security
	            int selectedRow = inDetailTV.getTable().getSelectedRow();
	            //CorporateAction ca = getSelectedItem(selectedRow);

//	            if (ca != null) {
//	                if (ca.seid != currSeId) {
//	                    SecurityObj secObj = SecurityMgr.getSingleSecurity(Integer.toString(ca.seid),"DB_IMS");
//	                    getSecClass().setClassification(secObj);
//
//	                    currSeId = ca.seid;
//	                }
//	            }
	        }
	    });

	    inDetailTV.getTable().addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent me) {
					if (SwingUtilities.isLeftMouseButton(me)) {
						Point point = me.getPoint();

						if (me.getClickCount() == 2) {
							
//							corpActTblDoSelect(inDetailTV.getTable().rowAtPoint(
//									point), inDetailTV.getTable().columnAtPoint(
//									point));
						}

					}
				}
			});
	    
	    
	  }
	  
	  protected void addInDetailTV()
	  {
	    centerPanel.add( inDetailTV,
			     new GridBagConstraints( 0,
						     0,
						     GridBagConstraints.REMAINDER,
						     GridBagConstraints.REMAINDER,
						     100.0,
						     100.0,
						     GridBagConstraints.CENTER,
						     GridBagConstraints.BOTH,
						     new Insets( 5, 5, 5, 5 ), 0, 0 ) );
	    centerPanel.validate();
	  }
	  
	  private void lookupCorpActions() {
		  Vector v=new Vector() ;
			System.out.println("Look up corp Action called ");
			 InventoryMgr obj =new InventoryMgr();
			  v=obj.getInventoryDetails();
			  lnDetailModel.setTableData(v);	
			  lnDetailModel.fireTableDataChanged();
			
		}


}
